package com.yanggu.spock.service;

import com.yanggu.spock.dao.MoneyDao;
import com.yanggu.spock.dao.UserDao;
import com.yanggu.spock.domain.entity.UserEntity;
import com.yanggu.spock.domain.vo.OrderVO;
import com.yanggu.spock.domain.vo.UserVO;
import com.yanggu.spock.exception.APIException;
import com.yanggu.spock.util.IDNumberUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Data
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private MoneyDao moneyDao;

    public UserVO getUserById(int uid) {
        List<UserEntity> users = userDao.getUserInfo();
        UserEntity userEntity = users.stream().filter(u -> u.getId() == uid).findFirst().orElse(null);
        UserVO userVO = new UserVO();
        if (userEntity == null) {
            return userVO;
        }
        userVO.setId(userEntity.getId());
        userVO.setName(userEntity.getName());
        userVO.setSex(userEntity.getSex());
        userVO.setAge(userEntity.getAge());
        // 显示邮编
        if ("上海".equals(userEntity.getProvince())) {
            userVO.setAbbreviation("沪");
            userVO.setPostCode(200000);
        }
        if ("北京".equals(userEntity.getProvince())) {
            userVO.setAbbreviation("京");
            userVO.setPostCode(100000);
        }
        // 手机号处理
        if (null != userEntity.getTelephone() && !userEntity.getTelephone().isEmpty()) {
            userVO.setTelephone(userEntity.getTelephone().substring(0, 3) + "****" + userEntity.getTelephone().substring(7));
        }

        return userVO;
    }

    public UserVO getUserByIdStatic(int uid) {
        List<UserEntity> users = userDao.getUserInfo();
        UserEntity userDTO = users.stream().filter(u -> u.getId() == uid).findFirst().orElse(null);
        UserVO userVO = new UserVO();
        if (null == userDTO) {
            return userVO;
        }
        userVO.setId(userDTO.getId());
        userVO.setName(userDTO.getName());
        userVO.setSex(userDTO.getSex());
        if ("上海".equals(userDTO.getProvince())) {
            userVO.setAbbreviation("沪");
            userVO.setPostCode(200000);
        }
        if ("北京".equals(userDTO.getProvince())) {
            userVO.setAbbreviation("京");
            userVO.setPostCode(100000);
        }
        if (null != userDTO.getTelephone() && !userDTO.getTelephone().isEmpty()) {
            userVO.setTelephone(userDTO.getTelephone().substring(0, 3) + "****" + userDTO.getTelephone().substring(7));
        }
        // 静态方法调用 身份证工具类
        Map<String, String> idMap = IDNumberUtils.getBirAgeSex(userDTO.getNo());
        userVO.setAge(idMap.get("age") != null ? Integer.parseInt(idMap.get("age")) : 0);
        return userVO;
    }

    /**
     * 根据汇率计算金额
     */
    public void setOrderAmountByExchange(UserVO userVO) {
        if (null == userVO.getUserOrders() || userVO.getUserOrders().isEmpty()) {
            return;
        }
        for (OrderVO orderVO : userVO.getUserOrders()) {
            BigDecimal amount = orderVO.getAmount();
            // 获取汇率(调用汇率接口)
            BigDecimal exchange = moneyDao.getExchangeByCountry(userVO.getCountry());
            // 根据汇率计算金额
            amount = amount.multiply(exchange);
            orderVO.setAmount(amount);
        }
    }

    /**
     * 校验请求参数user是否合法
     */
    public void validateUser(UserVO user) throws APIException {
        if (user == null) {
            throw new APIException("10001", "user is null");
        }
        if (null == user.getName() || user.getName().isEmpty()) {
            throw new APIException("10002", "user name is null");
        }
        if (user.getAge() == null) {
            throw new APIException("10003", "user age is null");
        }
        if (null == user.getTelephone() || user.getTelephone().isEmpty()) {
            throw new APIException("10004", "user telephone is null");
        }
        if (null == user.getSex()) {
            throw new APIException("10005", "user sex is null");
        }
        if (null == user.getUserOrders() || user.getUserOrders().isEmpty()) {
            throw new APIException("10006", "user order is null");
        }
        for (OrderVO order : user.getUserOrders()) {
            if (null == order.getOrderNum()) {
                throw new APIException("10007", "order number is null");
            }
            if (null == order.getAmount()) {
                throw new APIException("10008", "order amount is null");
            }
        }
    }

}